package en.edu.lzzy.bookmanagement.controller;

import en.edu.lzzy.bookmanagement.model.Book;
import en.edu.lzzy.bookmanagement.model.Category;
import en.edu.lzzy.bookmanagement.service.BookService;
import en.edu.lzzy.bookmanagement.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private CategoryService categoryService;

//    @GetMapping("/list")
//    public String getAllBooks(Model model) {
//        List<Book> books = bookService.getAllBooks();
//        model.addAttribute("books", books);
//        return "book-list";
//    }

    @GetMapping("/list")
    public String getAllBooks(Model model) {
        return findPaginated(1, 5,"id","desc", model);
    }

    @GetMapping("/add")
    public String showAddBookForm(Model model) {
        Book book = new Book();
        model.addAttribute("book", book);

        // 从数据库或其他数据源获取分类列表
        List<Category> categories = categoryService.getAllCategories();
        // 将分类列表传递给前端模板
        model.addAttribute("categories", categories);
        return "add-book";
    }

    @PostMapping("/add")
    public String addBook(@ModelAttribute("book") Book book) {
        bookService.saveBook(book);
        return "redirect:/books/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditBookForm(@PathVariable long id, Model model) {
        Book book = bookService.getBookById(id);
        model.addAttribute("book", book);
        // 从数据库或其他数据源获取分类列表
        List<Category> categories = categoryService.getAllCategories();
        // 将分类列表传递给前端模板
        model.addAttribute("categories", categories);
        return "edit-book";
    }

    @PostMapping("/edit/{id}")
    public String editBook(@PathVariable long id, @ModelAttribute("book") Book book) {
        book.setId(id);
        bookService.saveBook(book);
        return "redirect:/books/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteBook(@PathVariable long id) {
        bookService.deleteBookById(id);
        return "redirect:/books/list";
    }

//    @PostMapping("/search")
//    public String searchBooks(@RequestParam("keyword") String keyword, @RequestParam("searchBy") String searchBy, Model model) {
//        List<Book> books = bookService.searchBooks(keyword, searchBy);
//        model.addAttribute("books", books);
//        return "book-list";
//    }
    @PostMapping("/search")
    public String searchBooks(@RequestParam("keyword") String keyword,
                              @RequestParam("searchBy") String searchBy,
                              @PageableDefault(size = 999, sort = "id") Pageable pageable,
                              Model model) {
        Page<Book> bookPage = bookService.searchBooks(keyword, searchBy, pageable);
        model.addAttribute("books", bookPage.getContent());
        model.addAttribute("totalItems", bookPage.getTotalElements());
        model.addAttribute("currentPage", bookPage.getNumber() + 1);
        model.addAttribute("totalPages", bookPage.getTotalPages());
        return "book-list";
    }

    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,
                                @RequestParam("pageSize") int pageSize,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        Page<Book> page = bookService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<Book> books = page.getContent();
        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());
        model.addAttribute("books", books);
        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        return "book-list";
    }

}